<?php 

/**
 * InformesMantenimientoIncidencias
 * 
 * @package Sgit 
 * @author Juan Manuel Martinez Carrillo
 * @copyright Juan Manuel Martinez Carrillo
 * @version 2012
 * @access public
 * @revision 17/02/2012 12:19:36
 */


class InformesMantenimientoIncidenciasController extends Sgit_Controller_Action
{
	
    protected $_main_model='InformesMantenimientoIncidencias';
    protected $_main_form='InformesMantenimientoIncidencias';
    protected $_checkZona=true;


    function _begin()
    {
        $filter  = new Zend_Filter_Word_CamelCaseToUnderscore();
        $carpeta = $filter->filter($this->_controllerName);
        $this->_file_upload[1] = Sgit_Util_FileUpload::run($carpeta,$this->_idZona);
    }
    
    
     
	public function indexAction()
    {
        
        
        $filtros = new Sgit_Form_Filtros();

        $f_anio = new Sgit_Form_Element_Select_Year('anio');
        $filtros->addFormSelect($f_anio);
        
        $f_mes = new Sgit_Form_Element_Select_Mes('mes');
        $filtros->addFormSelect($f_mes);
        
        $filtros->addFormSelect($f_anio);
        $filtros->addFormSelect($f_mes);

        
        $values = $this->_getFiltrosValues($filtros);
        $filtros->myRender();
        
        if(is_numeric($values['anio'])) 
            $where['YEAR(fecha) = ?']=$values['anio'];
            
        if(is_numeric($values['mes'])) 
            $where['MONTH(fecha) = ?']=$values['mes'];   
            
        $where['id_zona = ?']=$this->_idZona;
        $rows=$this->_model->default->getAll($where);
       
        foreach($rows as $v){
            $filas[]=$this->_prepareValuesToTr($v);
        }
        
        
        $this->view->rows=$filas;

    }
    


    public function insertarAction()
    {
    	$this->_insertar();
    }

    public function editarAction()
    {
        $this->_editar();
    }
    
    public function borrarAction()
    {
        $this->_borrar();
    }

    protected function _prepareValuesToForm($row)
    {
        $row['fecha']=Sgit_Filter_DateDbToForm::filter($row['fecha']);
        return $row;
    }
    protected function _prepareValuesToTr($row)
    {
        
        $fecha=new Zend_Date($row['fecha'],Zend_Date::ISO_8601);
        $row['fecha']=$fecha->toString('MMM-yyyy');

        return $row;
    }
    
    protected function _prepareValuesToDb()
    {
        
        $row=$this->_form->getValues();
        $row['fecha']=Sgit_Filter_DateToDb::filter($row['fecha']);
        $row['id_zona']=$this->_idZona;
        
        
        Sgit_Util_removeFiles::run($this->_file_upload[1]['tabla'],$this->_file_upload[1]['key'],$this->_id,$row[$this->_file_upload[1]['activo']]);
        unset($row[$this->_file_upload[1]['name']]);
        unset($row[$this->_file_upload[1]['activo']]);
        
        return $row;
    }
    

}


